package com.jeecg.demo.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.jeecgframework.core.enums.StoreUploadFilePathEnum;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.FileUtils;
import org.jeecgframework.core.util.LogUtil;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.jwt.service.TokenManager;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.jwt.util.menu.ResponseMessageCodeEnum;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jeecg.demo.entity.AppFeedbackEntity;
import com.jeecg.demo.entity.AppLoginTokenEntity;
import com.jeecg.demo.entity.AppUserCollectionEntity;
import com.jeecg.demo.entity.AppVersionEntity;
import com.jeecg.demo.entity.GlCarEntity;
import com.jeecg.demo.entity.GlCargoEntity;
import com.jeecg.demo.service.AppAboutServiceI;
import com.jeecg.demo.service.AppFeedbackServiceI;
import com.jeecg.demo.service.AppLoginTokenServiceI;
import com.jeecg.demo.service.AppUserCollectionServiceI;
import com.jeecg.demo.service.GlCarServiceI;
import com.jeecg.demo.service.GlCargoServiceI;

/**
 * @Title: Controller
 * @Description:
 * @author onlineGenerator
 * @date 2018-08-23 14:58:22
 * @version V1.0
 * 
 */
@Api(value = "wulgl", description = "appinformation", tags = "appInfo")
@Controller
@RequestMapping("/wulgl")
public class Wuliugl {
    private static final Logger logger = LoggerFactory.getLogger(Wuliugl.class);
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private SystemService systemService;
    @Autowired
	private GlCarServiceI glCarService;
    @Autowired
    private GlCargoServiceI glCargoService;
    /**
     * addCar
     * 
     * @return
     */
    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "addCar", produces = "application/json", httpMethod = "POST")
    public ResponseMessage<?> addIdea(HttpServletRequest req,
            @RequestParam String  FULLNAME,
            @RequestParam String DIRECTION,
            @RequestParam String RATEDLOAD,
            @RequestParam String TRUCKNUM,
            @RequestParam String CONTACTER,
            @RequestParam String MOBILE,
            @RequestParam String TRUCKCAT,
            @RequestParam String  VALIDSTARTDATE,
            @RequestParam String  VALIDENDDATE,
            @RequestParam String FTMEMO,
            @RequestParam String USERID
    		) {
    	int id=generateId("GL_CAR");
    	GlCarEntity entity = new GlCarEntity();
    	entity.setId(id+"");
    	entity.setInfoid(id);
    	entity.setSysid(id);
    	entity.setFullname(FULLNAME);
    	entity.setDirection( DIRECTION);
    	entity.setRatedload( RATEDLOAD);
    	entity.setTrucknum(TRUCKNUM);
    	entity.setContacter(CONTACTER);
    	entity.setMobile( MOBILE);
    	entity.setTruckcat( TRUCKCAT);
    	
    	entity.setValidstartdate(strToDate(VALIDSTARTDATE));
    	entity.setValidenddate(strToDate( VALIDENDDATE));
    	entity.setRemark( FTMEMO);
    	entity.setUserid(Integer.parseInt(USERID));
    	entity.setReqstate("2");
    	entity.setReqtime(new Date());
    	entity.setInfosource("2");
        try {
        	glCarService.save(entity);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Result.error("提交失败");
        }
        return Result.success("提交成功");

    }
    /**
     * addGoods
     * 
     * @return
     */
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "addGoods", produces = "application/json", httpMethod = "POST")
    public ResponseMessage<?> addGoods(HttpServletRequest req,
            @RequestParam String FULLNAME,
            @RequestParam String CONTACTER,
            @RequestParam String MOBILE,
            @RequestParam String TRANSBEGINCITY,
            @RequestParam String TRANSENDCITY,
            @RequestParam String transbeginplace,
            @RequestParam String transendplace,
            @RequestParam String GOODSCAT,
            @RequestParam String TQUANTITY,
            @RequestParam String TPRICE,
            @RequestParam String SETTLEDES,
            @RequestParam String TRANSBEGINDATE,
            @RequestParam String TRANSENDDATE,
            @RequestParam String FTMEMO,
            @RequestParam String USERID
    		) {
    	int id=generateId("GL_CARGO");
    	GlCargoEntity entity = new GlCargoEntity();
    	entity.setId(id+"");
    	entity.setInfoid(id);
    	entity.setSysid(id);
    	entity.setFullname(FULLNAME);
    	entity.setContacter(CONTACTER);
    	entity.setMobile( MOBILE);
    	entity.setTransbegincityid(Integer.parseInt(TRANSBEGINCITY));
    	entity.setTransendcityid(Integer.parseInt(TRANSENDCITY));
    	entity.setTransbeginplace(transbeginplace);
    	entity.setTransendplace(transendplace);
    	entity.setGoodscat( GOODSCAT);
    	entity.setTquantity( TQUANTITY);
    	//构造以字符串内容为值的BigDecimal类型的变量bd 
    	BigDecimal bd=new BigDecimal(TPRICE); 
    	//设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入) 
    	bd=bd.setScale(2, BigDecimal.ROUND_HALF_UP); 
    	entity.setTprice(bd);
    	entity.setSettledes( SETTLEDES);
    	entity.setTransbegindate(strToDate(TRANSBEGINDATE));
    	entity.setTransenddate(strToDate(TRANSENDDATE));
    	entity.setFtmemo( FTMEMO);
    	entity.setUserid(Integer.parseInt(USERID));
    	entity.setReqstate("2");
    	entity.setReqtime(new Date());
    	entity.setInfosource("2");
    	entity.setTransportstate("0");
    	
    	
        try {
        	glCargoService.save(entity);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Result.error("提交失败");
        }
        return Result.success("提交成功");

    }
    /**
     * getGoodsList
     * 
     * @return
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "getGoodsList", produces = "application/json", httpMethod = "GET")
    public ResponseMessage<?> getGoodsList() {
        String sql = "select * from V_CARGO_REGION t order by t.reqtime desc";
        List<Map<String, Object>> listmap = systemService.findForJdbc(sql);
        if (null != listmap && listmap.size() > 0) {
            return Result.success(listmap);
        }
        return null;

    }

    /**
     * getCarList
     * 
     * @return
     */
    @RequestMapping(value = "/getCarList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "getCarList", produces = "application/json", httpMethod = "GET")
    public ResponseMessage<?> getCarList() {
        String sql = "select * from GL_CAR t order by t.reqstate desc ";
        List<Map<String, Object>> listmap = systemService.findForJdbc(sql);
        if (null != listmap && listmap.size() > 0) {
            return Result.success(listmap);
        }
        return null;

    }
   

    public int generateId(String tablename) {
    	 BigDecimal id = null;
        String sql = "select pagnbuilder.GetNextSysID(?) as id from dual";
        List<Map<String, Object>> list = systemService.findForJdbc(sql,
                tablename);
        if (null != list && list.size() > 0) {
            Map<String, Object> map = list.get(0);
            id= (BigDecimal) map.get("id");
         
         
        }

        return Integer.parseInt(id.toString());
    }
    /**
     * getMsgList
     * 
     * @return
     */
    @RequestMapping(value = "/getMsgList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "getMsgList", produces = "application/json", httpMethod = "GET")
    public ResponseMessage<?> getMsgList(@RequestParam("userid") String userid,@RequestParam("PAGES") int PAGES) {
    	
    	int begin=(PAGES-1)*10;
    			int end=PAGES*10;
        String sql = "select * from (SELECT A.*, ROWNUM RN from ( select * from SYS_APP_MESSAGESENDER t where t.receiver=? order by t.sendtime desc ) A WHERE ROWNUM <=?) WHERE RN > ?";
        List<Map<String, Object>> listmap = systemService.findForJdbc(sql,
                userid,end,begin);
        if (null != listmap && listmap.size() > 0) {
            return Result.success(listmap);
        }
        return null;

    }
    /**
     * 设置已读
     * 
     * @param phoneNum
     * @return
     */
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = " 设置已读", produces = "application/json", httpMethod = "POST")
    public ResponseMessage<?> updateState(
            @RequestParam("sysid") String sysid,
            @RequestParam("userid") String userid) {
        int result = 0;
        String updateSql = "update SYS_APP_MESSAGESENDER t  set t.msgstatus='2', t.readtime=? where  t.sysid=?  and t.receiver=? and t.msgstatus=1";
        result = systemService
                .executeSql(updateSql, new Date(), sysid, userid);
     
        return Result.success(result);
    }
    public Date strToDate(String time){
    	
    	
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		 Date dateTime = null;
		 try {
			 dateTime = simpleDateFormat.parse(time);
		 } catch (ParseException e) {
			 e.printStackTrace();
		 }
    	return dateTime;
    }
}
